package com.dhcc.sdc.busi.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.base.entity.SdcQcCdssLinkEntity;
import com.dhcc.sdc.base.service.ISdcQcCdssLinkService;
import com.dhcc.sdc.busi.dao.SdcQcMapper;
import com.dhcc.sdc.busi.entity.SdcQcEntity;
import com.dhcc.sdc.busi.service.ISdcQcService;
import com.dhcc.sds.base.dao.SdsQcentityMapper;
import com.dhcc.sds.base.entity.SdsQcentityEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


/**
 * 质控条目表数据服务层接口
 * 
 * @ClassName: SdcQcServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-07-26 14:24:20
 */
@Service("sdcQcService")
public class SdcQcServiceImpl extends BaseServiceImpl<SdcQcMapper, SdcQcEntity> implements ISdcQcService {

	@Resource
	private SdsQcentityMapper sdsQcentityMapper;
	@Autowired
	private ISdcQcCdssLinkService sdcQcCdssLinkService;

	@Override
	public List<SdcQcEntity> page(Page<SdcQcEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SdcQcEntity> list(Map<String, Object> map) {
		List<SdcQcEntity> sdcQcEntities = this.baseMapper.list(map);
		for (SdcQcEntity sdcQcEntity : sdcQcEntities){
			String linkCdss = sdcQcCdssLinkService.getLinkCdssByQcItemId(sdcQcEntity.getId());
			sdcQcEntity.setLinkCdss(linkCdss);
		}
		return sdcQcEntities;
	}

	@Override
	public List<Long> selectEntity() {
		return this.baseMapper.selectEntity();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void importItem(List<List<Object>> qcAll) {
		for (int i = 1; i < qcAll.size(); i++){
			List<Object> ls = qcAll.get(i);
			//病种
			String qcAbbrev = ls.get(0).toString();
			//质控条目
			String desc = ls.get(1).toString();
			//质控备注
			String memo = "";
			if (!StringUtils.isEmpty(ls.get(2))){
				 memo = ls.get(2).toString();
			}
			//表达式
			String expression="";
			if (!StringUtils.isEmpty(ls.get(3))){
				expression = ls.get(3).toString();
			}
			//表达式描述
			String expDescription = "";
			if (!StringUtils.isEmpty(ls.get(4))){
				expDescription = ls.get(4).toString();
			}
			String source = "";
			if (!StringUtils.isEmpty(ls.get(5))){
				source = ls.get(5).toString();
			}
			//触发一次
			Integer activeOnce = CommonUtil.equals(ls.get(6),"是")?1:0; // 1：是 0：否
			Integer isActive = CommonUtil.equals(ls.get(7),"是")?1:0;
			// 获取病种信息
			SdsQcentityEntity qcEntity = new SdsQcentityEntity();
			qcEntity.setAbbrev(qcAbbrev);
			qcEntity = this.sdsQcentityMapper.selectOne(qcEntity);
			if (qcEntity==null) {
				continue;
			}
			Long qcentityId = qcEntity.getId();

			SdcQcEntity sdcQcEntity = new SdcQcEntity();
			sdcQcEntity.setQcentityId(qcentityId);
			sdcQcEntity.setQcDesc(desc);

			sdcQcEntity = this.baseMapper.selectOne(sdcQcEntity);
			if (sdcQcEntity == null){
				sdcQcEntity = new SdcQcEntity();
				sdcQcEntity.setQcentityId(qcentityId);
				sdcQcEntity.setQcDesc(desc);
				sdcQcEntity.setQcMemo(memo);
				sdcQcEntity.setQcExpression(expression);
				sdcQcEntity.setActiveOnce(activeOnce);
				sdcQcEntity.setSource(source);
				sdcQcEntity.setExpDescription(expDescription);
				sdcQcEntity.setIsActive(isActive);
				sdcQcEntity.insert();
			}else {
				sdcQcEntity.setQcMemo(memo);
				sdcQcEntity.setQcExpression(expression);
				sdcQcEntity.setActiveOnce(activeOnce);
				sdcQcEntity.setSource(source);
				sdcQcEntity.setExpDescription(expDescription);
				sdcQcEntity.setIsActive(isActive);
				sdcQcEntity.updateAllColumnById();
			}
		}
	}

	@Override
	public JSONArray getJSONArrayByEntityId(Long entityId) {
		Map<String, Object> map = new HashMap<>();
		map.put("qcentityId", entityId);
		List<SdcQcEntity> list = this.baseMapper.list(map);
		JSONArray officeArry = new JSONArray();
		for(SdcQcEntity qcEntity : list){
			JSONObject officeObj = new JSONObject();
			officeObj.put("value", qcEntity.getId() + "");
			officeObj.put("text", qcEntity.getQcDesc());
			officeArry.add(officeObj);
		}
		return officeArry;
	}

}